package com.ruoyi.project.platform.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.framework.aspectj.lang.annotation.DataScope;
import com.ruoyi.project.platform.domain.PltUserOrderDetail;
import com.ruoyi.project.platform.dto.UserOrderDto;
import com.ruoyi.project.platform.mapper.PltUserOrderDetailMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.platform.mapper.PltUserOrderMapper;
import com.ruoyi.project.platform.domain.PltUserOrder;
import com.ruoyi.project.platform.service.IPltUserOrderService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 订单Service业务层处理
 *
 * @author ruoyi
 * @date 2020-07-27
 */
@Service
public class PltUserOrderServiceImpl implements IPltUserOrderService {
    @Autowired
    private PltUserOrderMapper pltUserOrderMapper;
    @Autowired
    private PltUserOrderDetailMapper pltUserOrderDetailMapper;

    /**
     * 查询订单
     *
     * @param orderId 订单ID
     * @return 订单
     */
    @Override
    public PltUserOrder selectPltUserOrderById(Long orderId) {
        return pltUserOrderMapper.selectPltUserOrderById(orderId);
    }

    /**
     * 查询订单列表
     *
     * @param pltUserOrder 订单
     * @return 订单
     */
    @Override
    @DataScope(userAlias = "u")
    public List<PltUserOrder> selectPltUserOrderList(PltUserOrder pltUserOrder) {
        return pltUserOrderMapper.selectPltUserOrderList(pltUserOrder);
    }

    /**
     * 新增订单
     *
     * @param pltUserOrder 订单
     * @return 结果
     */
    @Override
    public int insertPltUserOrder(PltUserOrder pltUserOrder) {
        return pltUserOrderMapper.insertPltUserOrder(pltUserOrder);
    }

    @Override
    @Transactional
    public int insertPltUserOrder(final UserOrderDto.OrderInfo pltUserOrder) {
        PltUserOrder p = new PltUserOrder();
        Date current = new Date();
        p.setCreatedTime(current.getTime());
        p.setUserId(pltUserOrder.getUserInfo().getUserId());
        p.setUserName(pltUserOrder.getUserInfo().getUserName());
        p.setUserPhone(pltUserOrder.getUserInfo().getUserPhone());
        p.setOrderStatus("init");
        p.setExpireTime(current.getTime() + 30 * 60 * 1000);
        p.setTotalPay(5000);
        insertPltUserOrder(p);
        if (!pltUserOrder.getRobotList().isEmpty()) {
            pltUserOrder.getRobotList().forEach(m -> {
                for (int i = 0; i < m.getNum().intValue(); i++) {
                    PltUserOrderDetail detail = new PltUserOrderDetail();
                    detail.setOrderId(p.getOrderId());
                    detail.setUserId(pltUserOrder.getUserInfo().getUserId());
                    detail.setUserName(pltUserOrder.getUserInfo().getUserName());
                    detail.setServiceId(m.getServiceId());
                    detail.setCreatedTime(current.getTime());
                    detail.setServiceCategory("robot");
                    detail.setServiceId(m.getServiceId());
                    detail.setServiceName(m.getServiceName() + "0" + i);
                    detail.setServiceStatus("init");
                    detail.setServiceType(m.getServiceType());
                    pltUserOrderDetailMapper.insertPltUserOrderDetail(detail);
                }
            });

        }

        if (!pltUserOrder.getStrategyList().isEmpty()) {
            pltUserOrder.getStrategyList().forEach(m -> {
                PltUserOrderDetail detail = new PltUserOrderDetail();
                detail.setOrderId(p.getOrderId());
                detail.setUserId(pltUserOrder.getUserInfo().getUserId());
                detail.setUserName(pltUserOrder.getUserInfo().getUserName());
                detail.setServiceId(m.getServiceId());
                detail.setCreatedTime(current.getTime());
                detail.setServiceCategory("strategy");
                detail.setServiceId(m.getServiceId());
                detail.setServiceName(m.getServiceName());
                detail.setServiceStatus("init");
                detail.setServiceType(m.getServiceType());
                pltUserOrderDetailMapper.insertPltUserOrderDetail(detail);
            });

        }

        return 1;
    }

    /**
     * 修改订单
     *
     * @param pltUserOrder 订单
     * @return 结果
     */
    @Override
    public int updatePltUserOrder(PltUserOrder pltUserOrder) {
        return pltUserOrderMapper.updatePltUserOrder(pltUserOrder);
    }

    /**
     * 批量删除订单
     *
     * @param orderIds 需要删除的订单ID
     * @return 结果
     */
    @Override
    public int deletePltUserOrderByIds(Long[] orderIds) {
        return pltUserOrderMapper.deletePltUserOrderByIds(orderIds);
    }

    /**
     * 删除订单信息
     *
     * @param orderId 订单ID
     * @return 结果
     */
    @Override
    public int deletePltUserOrderById(Long orderId) {
        return pltUserOrderMapper.deletePltUserOrderById(orderId);
    }

    @Override
    @Transactional
    public int payForOrder(Long orderId) {
        PltUserOrder order = pltUserOrderMapper.selectPltUserOrderById(orderId);
        if (order.getOrderStatus().equals("init")) {
            order.setOrderStatus("active");
            PltUserOrderDetail detail = new PltUserOrderDetail();
            detail.setOrderId(orderId);
            List<PltUserOrderDetail> details = pltUserOrderDetailMapper.selectPltUserOrderDetailList(detail);
            if(!details.isEmpty()){
                details.forEach(m->{
                    if(m.getServiceStatus().equals("init")){
                        m.setServiceStatus("actived");
                        pltUserOrderDetailMapper.updatePltUserOrderDetail(m);
                    }
                });
            }
            pltUserOrderMapper.updatePltUserOrder(order);
        }
        return 1;
    }
}
